package com.winbomb.kingcard.base.anim.impl;

import android.graphics.Color;

import com.winbomb.kingcard.IGameLogic;
import com.winbomb.kingcard.Pos;
import com.winbomb.kingcard.base.GameLogic;
import com.winbomb.kingcard.base.anim.IPlayable;
import com.winbomb.kingcard.base.anim.TransAnimation;
import com.winbomb.kingcard.framework.Graphics;

public class AttackAnim implements IPlayable {

	TransAnimation transAnim;
	private IGameLogic game;
	private Graphics g;
	private Pos srcPos;
	private Pos tgPos;

	/**
	 * 
	 * @param game
	 * @param srcPos
	 *            攻击发起的坐标
	 * @param destPos
	 *            攻击停止的坐标
	 */
	public AttackAnim(IGameLogic game) {
		this.game = game;
		this.g = ((GameLogic) game).getRender().getGraphics();
		this.srcPos = null;
		this.tgPos = null;
		this.transAnim = new TransAnimation(0, 0, 0, 0);
	}

	@Override
	public boolean play(float dt) {

		if (!transAnim.hasFinished()) {

			float x = transAnim.getCurrX();
			float y = transAnim.getCurrY();

			g.drawLine(srcPos.x, srcPos.y, (int) x, (int) y, Color.RED);

			transAnim.step(dt);
			return false;
		}

		boolean isSucceed = game.getBattle().fight();
		Pos pos = new Pos(tgPos.x - 27, tgPos.y - 27);
		AttackResultAnim resultAnim = new AttackResultAnim(game, pos, isSucceed);
		((GameLogic) game).getAnimManager().addAnimation(resultAnim);
		return true;
	}

	public void set(Pos srcPos, Pos destPos) {
		this.srcPos = srcPos;
		this.tgPos = destPos;

		transAnim.setStart(srcPos.x, srcPos.y);
		transAnim.setTarget(tgPos.x, tgPos.y);
		transAnim.setDuration(500);
	}

	public void reset() {
		this.srcPos = null;
		this.tgPos = null;
		transAnim.clear();
	}
}
